﻿using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace IMField
{    
    [CLSCompliant(false)]
    [Guid("dee44e9b-a3e4-41d4-96d5-1bb13ad1fc59")]
    public class IMFieldFieldControl : BaseFieldControl
    {
        private TextBox _tbICQ;
        private TextBox _tbSkype;
        private TextBox _tbYahoo;
        private TextBox _tbMessager;
        private HyperLink _lnkHelp;
        public override object Value
        {
            get
            {
                EnsureChildControls();
                SPFieldMultiColumnValue fieldValue = new SPFieldMultiColumnValue(4);
                fieldValue[0] = _tbICQ.Text.Trim();
                fieldValue[1] = _tbSkype.Text.Trim();
                fieldValue[2] = _tbYahoo.Text.Trim();
                fieldValue[3] = _tbMessager.Text.Trim();
                return fieldValue;
            }
            set
            {
                EnsureChildControls();
                SPFieldMultiColumnValue fieldValue = value as SPFieldMultiColumnValue;

                _tbICQ.Text = fieldValue[0];
                _tbSkype.Text = fieldValue[1];
                _tbYahoo.Text = fieldValue[2];
                _tbMessager.Text = fieldValue[3];
                base.Value = value;
            }
        }


        static string GetTextFromResources(string key) 
        {            
            return SPUtility.GetLocalizedString("$Resources:imfield, "+ key, "imfield", 
                SPContext.Current.Web.Language);
        }
        protected override void CreateChildControls()
        {
            if (Field == null)
                return;
            if (ControlMode == SPControlMode.Display)
                return;
            base.CreateChildControls();
            _tbICQ  = new TextBox();
            _tbSkype = new TextBox();
            _tbYahoo = new TextBox();
            _tbMessager = new TextBox();
            _lnkHelp = new HyperLink();

            if (ControlMode == SPControlMode.New)
            {
                // Fill default values
            }
            HtmlTable table = new HtmlTable();
            uint lang =  SPContext.Current.Web.Language;
            _lnkHelp.Text = GetTextFromResources("Help");



            _lnkHelp.NavigateUrl = GetLocalizeHelpUrl(lang);
            _lnkHelp.ToolTip = GetTextFromResources("HelpTooltip");                        
            CreateRow(table,GetTextFromResources("ICQ"),_tbICQ);
            CreateRow(table,GetTextFromResources( "Skype"), _tbSkype);
            CreateRow(table,GetTextFromResources( "Yahoo"), _tbYahoo);
            CreateRow(table, GetTextFromResources("LiveMess"), _tbMessager);
            CreateRow(table,"",_lnkHelp);

            base.Controls.Add(table);
        }

        private string GetLocalizeHelpUrl(uint lang)
        {
            string ret = "/_layouts/imfieldhelp.html";
            if(lang == 1049)
                ret = "/_layouts/imfieldhelpRu.html";
            return ret;
        }       
        private void CreateRow(HtmlTable table,string label,Control control)
        {
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            cell.InnerText = label;
            cell.Attributes["class"] = "ms-formdescription";
            row.Cells.Add(cell);
            cell = new HtmlTableCell();
            cell.Controls.Add(control);
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }
    }
}
